package com.hhf.rpa.glue;

import com.hhf.rpa.util.StringUtils;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class HandlerFactory<T> {
    public static HandlerFactory getInstance() {
        return new HandlerFactory();
    }

    public T build(HandlerParam param) {
        T instance = null;
        GlueTypeEnum glueTypeEnum = param.getGlueTypeEnum();
        if (glueTypeEnum == null) {
            return null;
        }
        switch(glueTypeEnum) {
            case BEAN :{
                instance = (T)SpringBeanUtils.getApplicationContext().getBean(param.getBeanName());
            }
            break;
            case GLUE_GROOVY: {
                try {
                    String glueCode = param.getGlueCode();
                    if (StringUtils.isNotBlank(glueCode)) {
                        instance = (T) SpringGlueFactory.getInstance().loadNewInstance(glueCode);
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    throw new RuntimeException(e);
                }
            }
            break;
        }
        return instance;
    }
}
